﻿using Dtos;
using IBLL;
using IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class FiniShWork : IFinishWorkBll
    {
        private IFinishWorkDal _dal;
        public FiniShWork(IFinishWorkDal dal)
        {
            _dal = dal;
        }
        public async Task<int> AddFinishWorkAsync(Guid homeworkId, Guid studentId, int workStatus)
        {
            return await _dal.AddAsync(new Model.FinishWork
            {
                HomeworkId = homeworkId,
                StudentId = studentId,
                WorkStatus = workStatus
            });
        }

        public async Task<int> DeleteFinishWorkAsync(Guid id)
        {
            var data = await _dal.GetById(id);
            return data != null ? await _dal.DeleteAsync(data) : 0;
        }

        public async Task<List<FinishWorkDto>> GetAll()
        {
            var data = await _dal.GetAll();
            return data.Select(f => new FinishWorkDto 
            { 
                Id = f.Id,
                WorkStatus = f.WorkStatus,
                HomeWorkId = f.HomeworkId,
                StudentId = f.StudentId,
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now,
            }).ToList();
        }

        public async Task<FinishWorkDto> GetFinishWorkByIdAsync(Guid id)
        {
            var data = await _dal.GetById(id);
            return new FinishWorkDto 
            {
                Id = data.Id,
                WorkStatus = data.WorkStatus,
                StudentId = data.StudentId,
                HomeWorkId =data.HomeworkId,
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now,
            };
        }

        public async Task<List<FinishWorkDto>> GetFinishWorkByNameAsync(int workStatus)
        {
            var data = await _dal.GetDataByChoose(f => f.WorkStatus == workStatus);
            if (data != null)
                return data.Select(f => new FinishWorkDto
                {
                    CreateTime = f.CreateTime,
                    UpdateTime = f.UpdateTime,
                    StudentId=f.StudentId,
                    WorkStatus = workStatus,
                    HomeWorkId=f.HomeworkId,
                    Id = f.Id,
                    DeleteId = f.DeleteId,
                }).ToList();
            return null;
        }

        public async Task<int> UpdateFinishWorkAsync(Guid id, int workStatus)
        {
            var data = await _dal.GetById(id);
            if(data == null)
                return 0;
            data.WorkStatus = workStatus;
            data.UpdateTime = DateTime.Now;
            return await _dal.UpdateAsync(data);
        }
    }
}
